//app.js
//const Promise = require('./utils/promise')
App({
  onLaunch: function () {
    // 展示本地存储能力
    const user_id = wx.getStorageSync('user_id') || ''
    this.globalData.user_id = user_id;
    let that = this;
    // 登录
    // console.log('s2')
    // that.login()
    //   .then(that.doLogin)
    //   .then(that.checkAuth('scope.userInfo'))
    //   .then(this.getUserInfo)
    //   .then(this.setUserData).catch(this.toLoad)
  },
  login: function () {
    const that = this;
    return new Promise((resolve, reject) => {
      if (that.globalData.user_id) {
        return resolve('[login]: has login')
      }
      wx.login({
        fail: res => {
          reject('network error');
        },
        success: res => {
          console.log('[login]:success')
          resolve(res);
        }
      })
    })
  },
  doLogin: function (res) {
    const that = this;
    console.log(res)
    return new Promise((resolve, reject) => {
      if (that.globalData.user_id) {
        return resolve('[login]: has login')
      }
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/onlogin',
        data: {
          code: res.code
        },
        success(res) {
          wx.setStorageSync('user_id', res.data.data.user_id)
          resolve(res);
        }
      })
    })
  },
  checkAuth: function (auth) {
    return res => {
      return new Promise((resolve, reject) => {
        wx.getSetting({
          fail: res => {
            console.log('[getSetting]: fail', auth)
            reject('[getSetting]: network error')
          },
          success: res => {
            if (res.authSetting[auth]) {
              console.log('[checkAuth]: success', auth)
              resolve(res)
            } else {
              reject(`[getSetting]: fail, ${auth}`)
            }
          }
        })
      })
    }
  },
  getUserInfo: function (res) {
    const that = this;
    return new Promise((resolve, reject) => {
      if (that.globalData.userInfo) {
        return resolve('[getUserInfo]: has get, ', that.globalData.userInfo)
      }
      wx.getUserInfo({
        success: res => {
          // 可以将 res 发送给后台解码出 unionId
          console.log('[getUserInfo]: has auth')
          that.globalData.userInfo = res.userInfo
          resolve(res)
        },
        fail: res => {
          reject('[getUserInfo]: network error')
        }
      })
    })
  },
  setUserData: function (res) {
    const that = this,
      user_id = this.globalData.user_id,
      {
        nickName,
        avatarUrl
      } = this.globalData.userInfo
    if (this.globalData.userData) {
      return Promise.resolve('[setUserData]: hasSet')
    }
    return new Promise((resolve, reject) => {
      if (!user_id || !nickName || !avatarUrl) {
        let reason = ''
        if (!user_id) {
          reason = '登录失败'
        } else if (!nickName) {
          reason = '获取昵称失败'
        } else {
          reason = '获取头像失败'
        }
        wx.showToast({
          icon: 'none',
          title: reason
        })
        return reject(`[setUserData]: fail,${reason}`)
      }
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/set_user_data',
        data: {
          user_id: user_id,
          user_nickname: nickName,
          user_avatarurl: avatarUrl
        },
        fail: res => {
          console.log(res)
          reject('[setUserData]: network error')
        },
        success: res => {
          if (res.data.ret == 200) {
            console.log('[setUserData]: success', res)
            resolve(res)
          } else {
            reject('[setUserData]: server error')
          }
        }
      })
    })
  },
  toLoad: function (res) {
    wx.redirectTo({
      url: '/pages/load/load',
    })
  },
  globalData: {
    user_id: '',
    userInfo: null,
    userData:null
  }
})